package com.vavr.checkincms.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.vavr.checkincms.domain.data.MeetingInfoDO;
import com.vavr.checkincms.domain.dto.MeetingConfigDTO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import java.util.List;
import java.util.Map;

/**
 * MeetingInfoMapper
 *
 * @since 2024.08
 */
@Mapper
public interface MeetingInfoMapper extends BaseMapper<MeetingInfoDO> {
    /**
     * 写入会议信息
     *
     * @param meetingInfoDO {@link MeetingInfoDO}
     * @return 写入条数
     */
    @Insert("INSERT INTO meeting_info (meeting_id, meeting_name, start_time, end_time, description, location, check_in_message, check_in_period) "
            + "VALUES (#{meetingId}, #{meetingName}, #{startTime}, #{endTime}, #{description}, #{location}, #{checkInMessage}, #{checkInPeriod})")
    @SelectKey(statement = "SELECT FLOOR((1 + (RAND() * 10000000)))", keyProperty = "meetingId", before = true, resultType = Integer.class)
    @Options(useGeneratedKeys = true, keyProperty = "meetingId", keyColumn = "meeting_id")
    int insertMeetingInfo(MeetingInfoDO meetingInfoDO);

    /**
     * 查询会议配置(根据会议id)
     *
     * @param meetingId {@link String}
     * @return List<MeetingConfigDTO>  会议配置信息
     */
    @Select(" select meeting_id,config_id,config_value,config_name,config_desc,sort "
            + " from  meeting_config where meeting_id = #{meetingId} ")

    @ResultType(MeetingConfigDTO.class)
    List<MeetingConfigDTO> queryMeetingInfo(@Param("meetingId") String meetingId);

    /**
     * 查询全部会议配置(根据配置id)
     *
     * @param configId {@link String}
     * @return List<MeetingConfigDTO>  会议配置信息
     */
    @Select(" select meeting_id,config_id,config_value,config_name,config_desc,sort "
            + " from  meeting_config where config_id = #{meetingId} ")

    @ResultType(MeetingConfigDTO.class)
    List<MeetingConfigDTO> queryMeetingConfigByConfigId(String configId);

    /**
     * 查询全部会议配置(根据配置id)
     *
     * @param meetingId {@link String}
     * @return List<MeetingConfigDTO>  会议配置信息
     */
    @Select(" select meeting_id,config_id,config_value,config_name,config_desc,sort "
            + " from  meeting_config where meeting_id like '%'||#{meetingId}||'%' "
            + "or (config_id ='meetingName' and  config_value like '%'||#{meetingId}||'%' ) ")
    @ResultType(MeetingConfigDTO.class)
    List<MeetingConfigDTO> queryAllMeeting(@Param("meetingId") String meetingId);

    /**
     * 查询全部会议配置(根据当前用户)
     *
     * @param meetingId {@link String}
     * @return List<MeetingConfigDTO>  会议配置信息
     */
    @Select(" select meeting_id,config_id,config_value,config_name,config_desc,sort "
            + " from  meeting_config where  meeting_id in ( select  meeting_id from meeting_relation where status = 1 and operator_id = #{operatorId} )   "
            + "and (  meeting_id like '%'||#{meetingId}||'%' "
            + "or (config_id ='meetingName' and  config_value like '%'||#{meetingId}||'%' ) ) ")
    @ResultType(MeetingConfigDTO.class)
    List<MeetingConfigDTO> queryMeetingByOperator(@Param("meetingId")String meetingId, @Param("operatorId")String operatorId);
}
